package com.shijie.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.shijie.core.AbstractService;
import com.shijie.domain.entity.Logs;
import com.shijie.mapper.LogsMapper;
import com.shijie.mapper.UserOrdersMapper;
import com.shijie.mapper.UsersMapper;
import com.shijie.utils.DateUtil;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@Service
public class LogsService extends AbstractService<Logs> {

	@Resource
	private LogsMapper logsMapper;
	@Resource
	private UsersMapper usersMapper;
	@Resource
	private UserOrdersMapper userOrdersMapper;

	public Map<String, List<Object>> statistics(Integer queryType, Integer count) {
		Map<String, List<Object>> map = new HashMap<>();
		map.put("times", new ArrayList<Object>());
		if (queryType == 0) {
			map.get("times")
					.addAll(DateUtil
							.getDaysBetweenStartAndEnd(DateTime.now()
									.minusDays(count).toDate(), DateTime.now()
									.toDate()));
		} else {
			map.get("times").addAll(
					DateUtil.getMonthsBetweenStartAndEnd(DateTime.now()
							.minusMonths(count).toDate(), DateTime.now()
							.toDate()));
		}
		map.put("ordercount", new ArrayList<Object>());
		map.put("usercount", new ArrayList<Object>());
		// 统计范围为过去30天的数据
		if (queryType == 0) {
			List<Map<String, Object>> days = usersMapper.queryStatisticByDays(
					DateTime.now().minusDays(count).toDate(), DateTime.now()
							.toDate());
			map.get("times").forEach(c -> {
				boolean flag = true;
				for (Map<String, Object> mapx : days) {
					if (c.equals(mapx.get("date"))) {
						map.get("usercount").add(mapx.get("count"));
						flag = false;
					}
				}
				if (flag) {
					map.get("usercount").add(0);
				}
			});
		} else {
			List<Map<String, Object>> months = usersMapper
					.queryStatisticByMonths(DateTime.now().minusDays(count)
							.toDate(), DateTime.now().toDate());
			map.get("times").forEach(c -> {
				boolean flag = true;
				for (Map<String, Object> mapx : months) {
					if (c.equals(mapx.get("date"))) {
						map.get("usercount").add(mapx.get("count"));
						flag = false;
					}
				}
				if (flag) {
					map.get("usercount").add(0);
				}
			});
		}

		if (queryType == 0) {
			List<Map<String, Object>> days = userOrdersMapper
					.queryStatisticByDays(DateTime.now().minusDays(count)
							.toDate(), DateTime.now().toDate());
			map.get("times").forEach(c -> {
				boolean flag = true;
				for (Map<String, Object> mapx : days) {
					if (c.equals(mapx.get("date"))) {
						map.get("ordercount").add(mapx.get("count"));
						flag = false;
					}
				}
				if (flag) {
					map.get("ordercount").add(0);
				}
			});
		} else {
			List<Map<String, Object>> months = userOrdersMapper
					.queryStatisticByMonths(DateTime.now().minusDays(count)
							.toDate(), DateTime.now().toDate());
			map.get("times").forEach(c -> {
				boolean flag = true;
				for (Map<String, Object> mapx : months) {
					if (c.equals(mapx.get("date"))) {
						map.get("ordercount").add(mapx.get("count"));
						flag = false;
					}
				}
				if (flag) {
					map.get("ordercount").add(0);
				}
			});
		}
		return map;
	}

}
